<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字" size="small"></el-input>
				</el-form-item>
<!--				<el-form-item>-->
<!--					<el-button type="primary" v-on:click="getTableData" icon="el-icon-search" size="small">查询</el-button>-->
<!--				</el-form-item>-->
				<el-form-item>
					<el-button type="primary"  icon="el-icon-plus" size="small"  v-on:click="add" >新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="tableData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
      <el-table-column prop="phone" label="电话"  >
      </el-table-column>
			<el-table-column prop="ip" label="ip"  >
			</el-table-column>
			<el-table-column prop="reason" label="拉黑原因"  >
			</el-table-column>

			<el-table-column label="操作" width="400">
				<template scope="scope">
          <el-button type="danger" size="small" @click="del(scope.row)" icon="el-icon-remove">移除黑名单</el-button>
					<el-button size="small"  @click="edit(scope.row)" icon="el-icon-edit" type="primary">编辑</el-button>
				</template>
			</el-table-column>
		</el-table>

    <!--拉黑界面-->
    <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" ref="addForm">
        <el-form-item label="电话" prop="phone">
          <el-input v-model="addForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="ip" prop="ip">
          <el-input v-model="addForm.ip" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="拉黑原因" prop="reason">
          <el-input v-model="addForm.reason" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false" icon="el-icon-close">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading" icon="el-icon-check">提交</el-button>
      </div>
    </el-dialog>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0" icon="el-icon-remove" size="small">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>


	</section>
</template>

<script>

	export default {
		data() {
			return {
				filters: {
					keyword: ""
				},
				tableData: [],
				total: 0,
				page: 1,
				listLoading: false,
				sels:[],
        //拉黑界面数据
        addForm: {
          phone: "",
          ip: "",
          reason: null
        },
        addFormVisible: false
			}

		},
		methods: {

      //显示编辑界面
      edit: function (row) {
        for(let p in this.addForm){
          this.addForm[p] = row[p];
        }
        this.addFormVisible = true;
      },

      //删除
      del: function (row) {
        this.$confirm('确认移除该记录吗?', '提示', { type: 'warning' }).then(() => {
          this.listLoading = true;
          this.$http.delete("/common/blackList/delete/"+row.id).then(result=>{
            let {success , data, message ,code} = result.data;
            if(success){
              this.$message({ message: "提交成功", type: 'success' });
              this.addFormVisible = false;
              this.getTableData();
            }else{
              this.$message({ message: "数据加载失败["+message+"]", type: 'error' });
            }
            this.listLoading = false;
          }).catch(error => {
            this.listLoading = false;
            this.$message({ message: "数据加载失败["+error.message+"]", type: 'error' });
          })
        });
      },

      //批量移除
      batchRemove: function () {
        var ids = this.sels.map(item => item.id);
        this.$confirm('确认移除选中记录吗？', '提示', { type: 'warning' }).then(() => {
          this.listLoading = true;
          this.$http.delete("/common/blackList/batch",ids).then(result=>{
            let {success , data, message ,code} = result.data;
            if(success){
              this.$message({ message: "提交成功", type: 'success' });
              this.addFormVisible = false;
              this.getTableData();
            }else{
              this.$message({ message: "数据加载失败["+message+"]", type: 'error' });
            }
            this.listLoading = false;
          }).catch(error => {
            this.listLoading = false;
            this.$message({ message: "数据加载失败["+error.message+"]", type: 'error' });
          })

        })
      },

			selsChange(sels){
				this.sels = sels;
			},
			handleCurrentChange(val) {
				this.page = val;
				this.getTableData();
			},
			//获取用户列表
			getTableData() {
				let para = {
					"page": this.page,
					"keyword": this.filters.keyword
				};
				this.listLoading = true; //显示加载圈
				this.$http.post("/common/blackList/pagelist",para).then(result=>{
					this.total = result.data.data.total;
					this.tableData = result.data.data.rows;
					this.listLoading = false;  //关闭加载圈
				}).catch(error => {
					this.listLoading = false;
					this.$message({ message: "数据加载失败["+error.message+"]", type: 'error' });

				})
			},
			add(){
				this.$message({ message: "功能未开放", type: 'error' });
			}

		},
		mounted() {
			this.getTableData();
		}
	}

</script>

<style scoped>

</style>